<?php
defined('IN_RONCRM') or exit('No permission resources.');
class MenuController extends CAdmin {
	
	private $db;
	
	function __construct(){
		parent::__construct();
		$this->db = new Menu_Model();
	}
	
	public function init(){
		$tree = new CTree();
		$tree->icon = array('&nbsp;&nbsp;│ ','&nbsp;&nbsp;├─ ','&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;';
		$result = $this->db->select('','*','','menu_id DESC');
		$array = array();
		foreach ($result as $r) {
			$r['str_mange'] ='<a href="?m=admin&c=menu&a=add&parentid='.$r['menu_id'].'" >添加子菜单</a> | <a href="?m=admin&c=menu&a=edit&menu_id='.$r['menu_id'].'">修改</a> | <a href="javascript:confirmurl(\'?m=admin&c=menu&a=delete&menu_id='.$r['menu_id'].'\',\'确定删除吗?\')">删除</a>';
			$array[] = $r;
		}
		$str = "<tr>
					<td>\$menu_id</td>
					<td style='text-align:left; padding-left:15px; font-size:15px;'>\$spacer \$name</td>
					<td>\$str_mange</td>
				</tr>";
		$tree->init($array);
		$categorys = $tree->get_tree(0, $str);
		include self::admin_tpl('menu_list');
	}
	
	public function add(){
		if(isset($_POST['dosubmit'])){
			$info = array();
			$menu_fields = array('parentid','name','m','c','a','display');
			$info = $this->checkinfo($_POST['info']);
			foreach($info as $key => $val){
				if(!in_array($key, $menu_fields)){
					unset($info[$key]);
				}
			}
			$this->db->insert($info);
			if($this->db->insert_id()){
				self::showmessage('操作成功！','?m=admin&c=menu');
			}			
		} else{
			$tree = new CTree();
			$result = $this->db->select();
			$array = array();
			$parentid = isset($_GET['parentid']) ? $_GET['parentid'] : 0;
			foreach ($result as $r){
				$r['selected'] = $r['menu_id'] == $parentid ? 'selected' : '';
	 			$array[] = $r;
			}
			$str = "<option value='\$menu_id' \$selected>\$spacer \$name</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			include self::admin_tpl('menu_add');
		}
	}
	
	public function edit(){
		if(isset($_POST['dosumbit'])){
			$menu_id = intval($_POST['menu_id']);
			$info = array();
			$menu_fields = array('parentid','name','m','c','a','display');
			$info = $this->checkinfo($_POST['info']);
			foreach($info as $key => $val){
				if(!in_array($key, $menu_fields)){
					unset($info[$key]);
				}
			}
			$this->db->update($info,array('menu_id'=>$menu_id));
			self::showmessage('操作成功！','?m=admin&c=menu');
		} else{
			$menu_id = intval($_GET['menu_id']);
			$menu = $this->db->get_one(array('menu_id'=>$menu_id));
			if($menu) extract($menu);
			$tree = new CTree();
			$result = $this->db->select();
			$array = array();
			foreach ($result as $r) {
				$r['selected'] = $r['menu_id'] == $parentid ? 'selected' : '';
				$array[] = $r;
			}
			$str = "<option value = '\$menu_id' \$selected> \$spacer \$name </option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			include self::admin_tpl('menu_edit');
		}
	}
	
	public function delete(){
		$menu_id = intval($_GET['menu_id']);
		$delete_menu = $this->db->get_one(array('menu_id'=>$menu_id),'m,c,a');
		//删除此菜单的相关权限
		$priv_db = new Admin_role_priv_Model();
		$priv_db->delete(array('m'=>$delete_menu['m'],'c'=>$delete_menu['c'],'a'=>$delete_menu['a']));
		$this->db->delete(array('menu_id'=>$menu_id));		
		self::showmessage('操作成功！','?m=admin&c=menu');
	}
	
	/**
	 * 检验数据
	 */
	private function checkinfo($data){
		$validator = new GlobalValidator();
		if(!is_array($data)){
			self::showmessage('参数不完整'); return false;
		} elseif(empty($data['name']) || empty($data['m']) || empty($data['c']) || empty($data['a']) ){
			self::showmessage('带[*]为必填项'); return false;
		}
		return $data;
	}
}